package handlers;
import java.io.IOException;
import java.io.InputStream;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;


public class PDFHandler implements DocumentHandler{
	
	public static String password = "-password";
	public void PDFBoxPDFHandler() {	}
	
	@SuppressWarnings("deprecation")
	public Document getDocument(InputStream is) throws Exception {
	COSDocument cosDoc = null;
	Document ret = new Document();
			
	try {
				cosDoc = parseDocument(is);
				
				if (!cosDoc.isEncrypted()) {
	//			//DecryptDocument decryptor = new DecryptDocument(cosDoc);
	//			//decryptor.decryptDocument(password);
	//			}
			// extract PDF document's textual content
				String docText = null;
				PDFTextStripper stripper = new PDFTextStripper();
				docText = stripper.getText(new PDDocument(cosDoc));
				Document doc = new Document();
				if (docText != null) {
					//doc.add(Field.UnStored("body", docText));
					doc.add(new Field("contents", docText, Field.Store.YES,Field.Index.TOKENIZED));
				}
				// extract PDF document's meta-data
				PDDocument pdDoc = new PDDocument(cosDoc);
			//	PDDocument.load(is);
				PDDocumentInformation docInfo =	pdDoc.getDocumentInformation();
				//PDDocumentInformation docInfo =	cosDoc.getDocumentInformation();
				String author = docInfo.getAuthor();
				String title = docInfo.getTitle();
				String keywords = docInfo.getKeywords();
				String summary = docInfo.getSubject();
				if ((author != null) && !author.equals("")) {
					//doc.add(Field.Text("author", author));
					doc.add(new Field("author", author, Field.Store.YES,Field.Index.TOKENIZED));
				}
				if ((title != null) && !title.equals("")) {
					//doc.add(Field.Text("title", title));
					doc.add(new Field("title", title, Field.Store.YES,Field.Index.TOKENIZED));
				}
				if ((keywords != null) && !keywords.equals("")) {
					//doc.add(Field.Text("keywords", keywords));
					doc.add(new Field("keywords", keywords, Field.Store.YES,Field.Index.TOKENIZED));
				}
				if ((summary != null) && !summary.equals("")) {
					//doc.add(Field.Text("summary", summary));
					doc.add(new Field("summary", summary, Field.Store.YES,Field.Index.TOKENIZED));
				}
				closeCOSDocument(cosDoc);
				closePDDocument(pdDoc);
				
				//return doc;
				ret = doc;
				}
			else{
				closeCOSDocument(cosDoc);
				ret = null;
			}
		}
			catch (Exception e) {
					//closeCOSDocument(cosDoc);
					//closePDDocument(pdDoc);
					System.err.println("Cannot get PDF document meta-data: "+ e.getMessage());
				}
				
			return ret;
	}
	
	private static COSDocument parseDocument(InputStream is)	throws IOException {
		PDFParser parser = new PDFParser(is);
		parser.parse();
		return parser.getDocument();
	}
	
	private void closeCOSDocument(COSDocument cosDoc) {
		if (cosDoc != null) {
			try {
				cosDoc.close();
			}
			catch (IOException e) {
				// eat it, what else can we do?
			}
		}
	}
	
	private void closePDDocument(PDDocument pdDoc) {
		if (pdDoc != null) {
			try {
				pdDoc.close();
			}
			catch (IOException e) {
				// eat it, what else can we do?
			}
		}
	}
	
}
